package com.tebiecloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.base.client.model.entity.SpreadTencent;
import com.tebiecloud.base.server.service.SpreadTencentService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 腾讯云推广信息 前端控制器
 * @author tebie
 * @date 2019-08-18
 */
@RestController
@RequestMapping(/"spreadtencent")
public class SpreadTencentController {

    @Autowired
    private SpreadTencentService spreadTencentService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<SpreadTencent>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        SpreadTencent query = pageParams.mapToObject(SpreadTencent.class);
        QueryWrapper<SpreadTencent> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(spreadTencentService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<SpreadTencent> get(@RequestParam("id") Long id) {
        SpreadTencent entity = spreadTencentService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param title 标题,
     * @param content 标题,
     * @param pic 图片地址,
     * @param link 跳转链接地址,
     * @param type 1热门推荐 2更多优惠 3相关文章,
     * @param status 0失效1生效,
     * @param sort 排序值，数值越小排越前
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "title", required = false) String title,
        @RequestParam(value = "content", required = false) String content,
        @RequestParam(value = "pic", required = false) String pic,
        @RequestParam(value = "link", required = false) String link,
        @RequestParam(value = "type", required = false) Integer type,
        @RequestParam(value = "status", required = false) Integer status,
        @RequestParam(value = "sort", required = false) Integer sort
    ) {
        SpreadTencent entity = new SpreadTencent();
        entity.setTitle(title);
        entity.setContent(content);
        entity.setPic(pic);
        entity.setLink(link);
        entity.setType(type);
        entity.setStatus(status);
        entity.setSort(sort);
        spreadTencentService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param title 标题
     * @param content 标题
     * @param pic 图片地址
     * @param link 跳转链接地址
     * @param type 1热门推荐 2更多优惠 3相关文章
     * @param status 0失效1生效
     * @param sort 排序值，数值越小排越前
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "content", required = false) String content,
            @RequestParam(value = "pic", required = false) String pic,
            @RequestParam(value = "link", required = false) String link,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "sort", required = false) Integer sort
    ) {
        SpreadTencent entity = new SpreadTencent();
        entity.setId(id);
        entity.setTitle(title);
        entity.setContent(content);
        entity.setPic(pic);
        entity.setLink(link);
        entity.setType(type);
        entity.setStatus(status);
        entity.setSort(sort);
        spreadTencentService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            spreadTencentService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            spreadTencentService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
